package global

import (
	"fmt"
	"github.com/sirupsen/logrus"
	"github.com/spf13/viper"
	"gorm.io/gorm"
	"simple-app/lib/db"
	"simple-app/lib/log"
	"simple-app/structs/base"
)

var (
	SysConf = new(base.SysConf)
	DB      *gorm.DB
)

func InitSysConfig(configName string) {
	v := viper.New()
	v.SetConfigType("yaml")
	v.SetConfigName(configName)
	v.AddConfigPath(".")
	err := v.ReadInConfig()
	if err != nil {
		panic(fmt.Sprintf("加载配置文件失败： %v", err))
	}
	err = v.Unmarshal(SysConf)
	if err != nil {
		panic(fmt.Sprintf("不合法的配置信息： %v", err))
	}
	log.InitLog(SysConf.Log.Name, SysConf.Log.Lv)
}

func InitDB() {
	var err error
	DB, err = db.GetDB(SysConf.DB.Supper, SysConf.DB.ShowSqlLog, SysConf.DB.Source)
	if err != nil {
		logrus.Panicf("连接数据库失败：%v", err)
	}
	autoMigrate()
}
